import Block.Block;
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import Block.*;
/**
* Created with IntelliJ IDEA.
* User: Bram
* Date: 11-6-13
* Time: 20:04
* To change this template use File | Settings | File Templates.
*/
public class MineSweeper extends Rectangle {
private HashMap<String, BufferedImage> icons;
private Block[][] blocks;
private int blockSize = 16;
private BufferedImage image;
private ImageHandler imageHandler;
private Robot robot;
private boolean inProgress;
public MineSweeper(int amountOfWidthBlocks, int amountOfHeightBlocks) throws MineSweeperNotVisibleException, AWTException {
robot = new Robot();
imageHandler = new ImageHandler(robot);
generateBlocks(amountOfWidthBlocks, amountOfHeightBlocks);
loadIcons();
updateLocation(amountOfWidthBlocks);
setBounds(x, y, amountOfWidthBlocks*blockSize, amountOfHeightBlocks*blockSize);
}
public boolean isInProgress() {
return inProgress;
}
public void setInProgress(boolean inProgress) {
this.inProgress = inProgress;
}
public void update(){
image = imageHandler.getScreenImage(this);
for(int c=0; c<blocks.length; c++)
for(int r=0; r<blocks[0].length; r++)
updateBlock(blocks[c][r]);
}
private void loadIcons() {
icons = new HashMap<String, BufferedImage>();
icons.put("start", imageHandler.loadImageFromFile("smile.png"));
icons.put("lose", imageHandler.loadImageFromFile("sad.png"));
icons.put("win", imageHandler.loadImageFromFile("smile.png"));
}
private void updateLocation(int amountOfWidthBlocks) throws MineSweeperNotVisibleException {
Point iconLocation = null;
for(BufferedImage icon : icons.values()){
iconLocation = imageHandler.searchImageInImage(imageHandler.getScreenImage(), icon);
if(iconLocation!=null)
break;
}
if(iconLocation==null)
throw new MineSweeperNotVisibleException("Minesweeper is not visible.");
System.out.println((amountOfWidthBlocks/2*16)-13);
x = iconLocation.x - ((amountOfWidthBlocks/2*16)-13);
y = iconLocation.y + 40;
}
private void generateBlocks(int columns, int rows) {
blocks = new Block[columns][rows];
for(int c=0; c<columns; c++)
for(int r=0; r<rows; r++)
blocks[c][r] = new Block(c*blockSize, r*blockSize, blockSize, blockSize);
}
public Block[][] getBlocks() {
return blocks;
}
private void updateBlock(Block block){
//Reset the block
block.reset();
setBlockState(block);
if(block.getBlockState()!=null)
return;
for(int x=block.x+2; x<block.x+blockSize-2; x++){
int rgbTop = image.getRGB(x, block.y+4);
int rgbBot = image.getRGB(x, block.y+12);
if(rgbTop==rgbBot & setBlockNumber(rgbTop, block))
break;
}
block.setBlockState(BlockState.EMPTY);
}
private void setBlockState(Block block) {
//Detect and set the block state
if(image.getRGB(block.x + 7, block.y + 7)==Color.WHITE.getRGB()){
block.setBlockState(BlockState.BOMB);
inProgress=false;
}
else if(image.getRGB(block.x + 7, block.y + 5)==Color.RED.getRGB() & image.getRGB(block.x + 8, block.y + 12)==Color.BLACK.getRGB())
block.setBlockState(BlockState.FLAG);
else if(image.getRGB(block.x,block.y)==Color.WHITE.getRGB())
block.setBlockState(BlockState.UNCHECKED);
}
private boolean setBlockNumber(int rgb, Block block) {
for(BlockNumber blockNumber : BlockNumber.values())
if(rgb == blockNumber.getRgbValue()){
block.setBlockNumber(blockNumber);
return true;
}
return false;
}
public void clickBlock(int column, int row, int key) {
clickBlock(blocks[column][row], key);
}
public void clickBlock(Block block, int key){
robot.mouseMove(x + block.x + blockSize / 2, y + block.y + blockSize / 2);
robot.mousePress(key);
sleep(10);
robot.mouseRelease(key);
sleep(20);
if(key==InputEvent.BUTTON3_MASK)
block.setBlockState(BlockState.FLAG);
}
private void sleep(int i) {
try {
Thread.sleep(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void clickMiddleMouseButton(Block block){
robot.mouseMove(x+block.x+blockSize/2, y+block.y+blockSize/2);
robot.mousePress(KeyEvent.BUTTON3_DOWN_MASK);
sleep(10);
robot.mouseRelease(KeyEvent.BUTTON3_DOWN_MASK);
}
private Block getBlockByCordinates(int x, int y) {
for(int c=0; c<blocks.length; c++)
for(int r=0; r<blocks[0].length; r++)
if(blocks[c][r].contains(x, y))
return blocks[c][r];
return null;
}
public BufferedImage getImage() {
return image;
}
}